#    One Room And A Goblin Invasion
#    Copyright (C) 2014  Jannik Haberbosch
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
pygame.init()

import math


class CollisionLine( object ):
	"""

	"""
	test = []

	
	def __init__( self,
		x1,
		y1,
		x2,
		y2 ):
		"""

		"""
		self.x1 = x1
		self.y1 = y1
		self.x2 = x2
		self.y2 = y2

		self.slope = ( self.y2 - self.y1 ) / ( self.x2 - self.x1 )
		self.intercept = self.y1 - ( self.slope * self.x1 )

		self.rect = pygame.Rect(
			self.x1,
			self.y1,
			math.fabs( self.x2 - self.x1 ),
			math.fabs( self.y2 - self.y1 )
		)

		# Position the rectangle.
		if self.y1 > self.y2 and self.x1 < self.x2:
			self.rect.bottom += self.y2 - self.y1
		elif self.y1 < self.y2 and self.x1 < self.x2:
			pass


		self.image = pygame.Surface(
			(
				math.fabs( self.x1 - self.x2 ),
				math.fabs( self.y1 - self.y2 )
			)
		)
		self.image.fill( ( 100, 100, 100 ) )

		self.color = pygame.Color( 0, 0, 0, 0 )


		# Mainline
		self.A1 = self.y2 - self.y1
		self.B1 = self.x1 - self.x2
		self.C1 = self.A1 * self.x1 + self.B1 * self.y1		



	def update( self ):
		pass


	@staticmethod
	def blit( window, lines ):
		return
		
		for l in lines:
			#window.blit( l.image, l.rect )
			pygame.draw.line(
				window,
				l.color,
				(
					l.x1,
					l.y1
				),
				(
					l.x2,
					l.y2
				),
				10
			)


	@staticmethod
	def collision_with_projectiles(
		lines,
		projectiles ):
		"""

		"""
		tolerance = 5 # px
		
		for l in lines:
			for p in projectiles:
				orig_y = p.rect.centery
				calc_y = l.slope * p.rect.centerx + l.intercept

				test = orig_y - calc_y

				if ( tolerance * -1 ) < test and test < tolerance:
					# Does the point lie on the line segment?
					if l.rect.colliderect( p.rect ):
						p.kill()
						CollisionLine.test.append( p )
